
function dealCorrect_item(data: any) { 
    if (!data.correct_item) { 
        return null
    }
    const value = JSON.parse(data.correct_item)
    if (data.question_style === 1) { 
        return data.item.find((item: any) => item.option === value[0].option).value
    }
    if (data.question_style === 2) { 
        return data.judgment.find((item : any) => item.key === value.key).value
    }
    if (data.question_style === 3) { 
        const optionVal = value.map((o: any) => o.option)
        const values = data.item.filter((item: any) => { 
            if (optionVal.includes(item.option)) { 
                return item.option
            }
        })
        return values.map((item: any) => item.value)
    }
    if (data.question_style === 4) { 
        data.analysis = value[0].value
        return value[0].value
    }

}

export default function getData(data: []) { 
    return data.map(p => {
        const {
            question_style, //  1单选 2判断题  3多选  4填空
            sort,
            title_thumb, // 标题图片
            analyze_thumb, // 答案图片
            judgment, //判断选项
        title, // 标题
        fraction, // 分值
        question_id,
        correct_item, // 选择题答案
        analysis, // 非选择题答案
        item, // 选择题options
        content,
        child
        } = p as any

        item && item.forEach((o: any) => {
          o.value = o.value || o.img
        })
        judgment && judgment.forEach((o: any) => {
          o.value = o.text
        })

        return {
            val: (question_style === 3 ? [] : '') as any,
            error: false,
            sort,
            title_thumb, // 标题图片
            analyze_thumb, // 答案图片
            question_style: question_style === 2 ? 1 : question_style, //  1单选 2判断题  3多选  4填空
            title, // 标题
            fraction: Number(fraction), // 分值
            question_id,
            correct_item: dealCorrect_item(p), // 选择题正确答案
            analysis: analysis.replace('解得：', '').replace('证明：', '').replace('解：', ''), // 非选择题正确答案
            item: item || judgment, // 选择题options
            content,
            child: child.map((c: any) => { 
                const citem = JSON.parse(c.correct_item)
                return {
                    fraction: Number(c.fraction),
                    title: c.title,
                    correct_item: citem[0].value || dealCorrect_item(c),
                    question_style: c.question_style,
                    item: c.item,
                    question_id: c.question_id,
                    val: ''
                }
            }),
        }
    })
        .sort((a, b) => a.sort - b.sort)
 }